package com.wechat.shopping.mall.category.service.impl;

import com.wechat.shopping.mall.bean.po.CategoryGoodRelation;
import com.wechat.shopping.mall.bean.po.WechatShoppingCategories;
import com.wechat.shopping.mall.bean.po.WechatShoppingCategoriesExample;
import com.wechat.shopping.mall.category.repository.CategoryMapper;
import com.wechat.shopping.mall.category.service.CategoryService;
import com.wechat.shopping.mall.po.CategoryGoodRelationMapper;
import com.wechat.shopping.mall.po.WechatShoppingCategoriesMapper;
import com.wechat.shopping.mall.po.WechatShoppingGoodsMapper;
import com.wechat.shopping.mall.util.BusinessException;
import com.wechat.shopping.mall.util.Constant;
import com.wechat.shopping.mall.util.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class CategoryServiceImpl implements CategoryService{
    private static final Logger LOG = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Autowired
    private WechatShoppingCategoriesMapper wechatShoppingCategoriesMapper;

    @Autowired
    private CategoryGoodRelationMapper categoryGoodRelationMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private WechatShoppingGoodsMapper wechatShoppingGoodsMapper;

    @Override
    @Transactional(readOnly = false)
    public ResponseMessage saveCategory(String categoryName, Byte type, Integer parentId) {
        try {
            //插入分类表
            WechatShoppingCategories wechatShoppingCategories = new WechatShoppingCategories();
            wechatShoppingCategories.setGoodCategoryName(categoryName);
            wechatShoppingCategories.setType(type);
            wechatShoppingCategoriesMapper.insertSelective(wechatShoppingCategories);
            //分类关系表
            CategoryGoodRelation categoryGoodRelation = new CategoryGoodRelation();
            categoryGoodRelation.setGoodsCategoriesId(wechatShoppingCategories.getGoodsCategoriesId());
            categoryGoodRelation.setParentId(parentId);
            categoryGoodRelationMapper.insertSelective(categoryGoodRelation);
            return ResponseMessage.ok();
        } catch (Exception e) {
            LOG.error("新建分类信息错误",e);
            throw new BusinessException("分类信息创建失败");
        }
    }

    @Override
    @Transactional(readOnly = false)
    public int editCategory(Integer id, String categoryName) {
        WechatShoppingCategories wechatShoppingCategories = new WechatShoppingCategories();
        wechatShoppingCategories.setGoodCategoryName(categoryName);
        wechatShoppingCategories.setGoodsCategoriesId(id);
        return wechatShoppingCategoriesMapper.updateByPrimaryKeySelective(wechatShoppingCategories);
    }

    @Override
    public int deleteCategory(Integer id) {
        return wechatShoppingCategoriesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ResponseMessage getCategories(Integer categoryId, Byte type) {
        WechatShoppingCategoriesExample categoriesExample = new WechatShoppingCategoriesExample();
        List<WechatShoppingCategories> wechatShoppingCategories = new ArrayList<>();
        //判断是不是顶级菜单
        if (Constant.ONE_BYTE.equals(type)) {
            categoriesExample.createCriteria().andTypeEqualTo(Constant.ONE_BYTE)
                    .andIsdelEqualTo(Constant.ZERO_BYTE);
            wechatShoppingCategories = wechatShoppingCategoriesMapper.selectByExample(categoriesExample);
        }else {
            wechatShoppingCategories = categoryMapper.getCategoriesByType(categoryId, type);
        }
        return ResponseMessage.ok(wechatShoppingCategories);
    }

    @Override
    public ResponseMessage getGoodById(Integer goodId) {
        return ResponseMessage.ok(wechatShoppingGoodsMapper.selectByPrimaryKey(goodId));
    }


}
